package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.Export;
import cn.itcast.domain.cargo.ExportExample;
import cn.itcast.domain.cargo.Packing;
import cn.itcast.domain.cargo.PackingExample;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/cargo/packing")
public class PackingController extends BaseController {

    @Reference
    private PackingService packingService;


    /*
    url:/cargo/packing/list.do
    作用：点击装箱管理，进入装箱管理列表
    参数：
    */
    @RequestMapping("/list")
    public String findAll(@RequestParam(defaultValue  = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize){
        PackingExample packingExample = new PackingExample();
        packingExample.setOrderByClause("create_time desc");//desc 降序、 asc 升序
        packingExample.createCriteria().andCompanyIdEqualTo(getLoginUserCompanyId());
        PageInfo<Packing> pageInfo = packingService.findByPage(packingExample, pageNum, pageSize);
        request.setAttribute("pageInfo",pageInfo);
        return "cargo/packing/packing-list";
    }

    /*
       url:/cargo/packing/save.do
       作用：保存，新增装箱单
       参数：packing对象
    */
    @RequestMapping("/save")
    public String save(Packing packing){
        //装箱单的创建人
        packing.setCreateBy(getLoginUser().getId());
        //装箱单的创建人所属的部门
        packing.setCreateDept(getLoginUser().getDeptId());
        //装箱单的创建人所属的企业id
        packing.setCompanyId(getLoginUserCompanyId());
        //装箱单的创建人所属的企业名称
        packing.setCompanyName(getLoginUserCompanyName());

        packingService.save(packing);
        return "redirect:/cargo/packing/list.do";
    }

    /*
       url:${ctx}/cargo/packing/delete.do?packingListId="+id
       作用：删除装箱单
       参数：装箱单id
    */
    @RequestMapping("/delete")
    @ResponseBody
    public String delete(String packingListId){
        //装箱单状态为草稿的时候才能删除
        Packing packing = packingService.findById(packingListId);
        if (packing.getState()==0){
            packingService.delete(packingListId);
            return "true";
        }else {
            return "false";
        }
    }

    /*
       url:${ctx}/cargo/packing/toView.do?packingListId="+id
       作用：保存，新增装箱单
       参数：packing对象
    */
    @RequestMapping("/toView")
    public String toView(String packingListId){
        //装箱单的创建人
        Packing packing = packingService.findById(packingListId);
        request.setAttribute("packing",packing);
        request.setAttribute("id",packingListId);
        return "cargo/packing/packing-view";
    }

    @RequestMapping("/toShipping")
    @ResponseBody
    public String toShipping(String packingListId) {
        Packing packing = packingService.findById(packingListId);
        if (packing.getState() == 0) {
            return "true";
        } else {
            return "false";
        }
    }
}
